package com.ruoyi.crm.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author YanWenliang
 * @date 2025/1/7 9:53
 * @desciption:
 */
public class ThreadLocalUtils<T> {
    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL_MAP = ThreadLocal.withInitial(HashMap::new);

    /**
     * 存储指定类型的对象到 ThreadLocal
     */
    public static <T> void set(String key, T obj) {
        THREAD_LOCAL_MAP.get().put(key, obj);
    }

    /**
     * 获取指定类型的对象
     */
    public static <T> T get(String key) {
        Object o = THREAD_LOCAL_MAP.get().get(key);
        return (T) o; // 泛型转换
    }

    /**
     * 清除指定类型的对象
     */
    public static <T> void remove(String key) {
        THREAD_LOCAL_MAP.get().remove(key);
    }

    /**
     * 清除所有存储的对象
     */
    public static void clear() {
        THREAD_LOCAL_MAP.remove();
    }
}
